// 获取应用实例
const app = getApp();

// 初始化数据库
const db = wx.cloud.database();
const message = db.collection("message");

// pages/search/search.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    theme: 1,
    zone: 1,
    msgList: [],
    searchValue: "",
    afterSearch: [],
    openid: "",
    avatarUrl: "",
    nickname: "",
    o_openid: "",
    navHeight: app.globalData.navBarHeight,
    menuHeight: app.globalData.menuHeight,
    menuRight: app.globalData.menuRight,
    menuTop: app.globalData.menuTop,

    showTextarea: "",
    showLongpress: "",
    auto_focus: false,
    type: "",
    clicktime: "",
    _time: "",
  },

  // 获取数据
  getMsgData(openid) {
    message.where({ _openid: openid }).get({
      success: (res) => {
        this.setData({
          msgList: res.data[0].myMsg,
          avatarUrl: res.data[0].avatarUrl,
          nickname: res.data[0].nickname,
        });
      },
      catch: () => {
        wx.showToast({
          title: "获取失败",
          icon: "none",
        });
      },
    });
  },

  // 搜索
  search(e) {
    let { msgList, searchValue, afterSearch } = this.data;
    searchValue = e.detail;
    if (searchValue) {
      afterSearch = msgList.filter((v) => v.content.indexOf(searchValue) >= 0);
      this.setData({
        afterSearch,
        searchValue,
      });
    } else {
      this.setData({
        afterSearch: [],
      });
    }
  },

  // 删除留言
  del(e) {
    const { time } = e.currentTarget.dataset;
    let { openid, msgList, afterSearch } = this.data;
    const that = this;
    wx.showModal({
      title: "确认删除",
      content: "请确认是否要删除此留言，一经删除不可恢复",
      success: (result) => {
        // if (result.confirm) {
        //   message.doc(id).remove({
        //     success: function (res) {
        //       wx.showToast({
        //         title: "删除成功",
        //         icon: "success",
        //       });
        //       that.setData({
        //         searchValue: "",
        //         afterSearch: [],
        //       });
        //     },
        //   });
        // }
        if (result.confirm) {
          msgList = msgList.filter((i) => i.time != time);
          afterSearch = afterSearch.filter((i) => i.time != time);
          message.where({ _openid: openid }).update({
            data: {
              myMsg: msgList,
            },
            success: function (res) {
              wx.showToast({
                title: "删除成功",
                icon: "success",
              });
              that.setData({
                afterSearch,
              });
            },
            fail: (err) => {
              wx.showToast({
                title: "删除失败",
                icon: "error",
              });
            },
          });
        }
      },
    });
  },

  touchstart() {
    this.setData({
      showLongpress: "",
      showTextarea: "",
    });
  },

  longpress(e) {
    const { time } = e.currentTarget.dataset;
    this.setData({
      showLongpress: time,
    });
  },

  delComment(e) {
    const { time, _time, type } = e.currentTarget.dataset;
    const { zone, openid, o_openid } = this.data;
    const that = this;

    switch (type) {
      case "delComment":
        if (zone == 1) {
          message.where({ _openid: openid }).get({
            success: (res) => {
              let { myMsg } = res.data[0];
              console.log("对方", myMsg);
              myMsg.forEach((item) => {
                if (item.time == time) {
                  item.comment = item.comment.filter((i) => i.time != _time);
                  return;
                }
              });
              message.where({ _openid: openid }).update({
                data: {
                  myMsg,
                },
                success: () => {
                  wx.showToast({
                    title: "删除成功",
                    icon: "none",
                  });
                  that.setData({
                    msgList: myMsg,
                  });
                },
              });
            },
          });
        } else {
          message.where({ _openid: o_openid }).get({
            success: (res) => {
              let { myMsg } = res.data[0];
              console.log("对方", myMsg);
              myMsg.forEach((item) => {
                if (item.time == time) {
                  item.comment = item.comment.filter((i) => i.time != _time);
                  return;
                }
              });
              console.log(myMsg);

              message.where({ _openid: o_openid }).update({
                data: {
                  myMsg,
                },
                success: () => {
                  wx.showToast({
                    title: "删除成功",
                    icon: "none",
                  });
                  that.setData({
                    msgList: myMsg,
                  });
                },
              });
            },
          });
        }
        break;

      case "delReply":
        wx.showToast({
          title: "开发中",
          icon: "none",
        });
        break;
    }
  },

  replyComment(e) {
    const { time, _time, type } = e.currentTarget.dataset;
    this.setData({
      showTextarea: time,
      auto_focus: true,
      showLongpress: "",
      type,
      clicktime: time,
      _time,
    });
  },

  copyCon(e) {
    const { con } = e.currentTarget.dataset;
    wx.setClipboardData({
      data: con,
      success(res) {
        wx.showToast({
          title: '复制成功',
          icon: 'none'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let { openid, o_openid } = this.data;
    openid = wx.getStorageSync("openid");
    o_openid = wx.getStorageSync("o_openid");
    this.getMsgData(openid);
    this.setData({
      openid,
      o_openid,
      theme: app.globalData.theme,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
